package com.feng.ems.controller;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.feng.ems.bean.Book;
import com.feng.ems.service.BookService;
import com.feng.ems.service.BookServiceImpl;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.jws.WebParam;
import javax.servlet.http.HttpServletRequest;

@Controller
public class CrudController {
    @Autowired
    BookServiceImpl bookService;

    @GetMapping("/showemp")
    public String selectAll(@RequestParam(value = "pn",defaultValue = "1")Integer pn,
                            Model model){
        Page<Book> page = new Page<>(pn,20);
        Page<Book> bookPage = bookService.page(page, null);
        model.addAttribute("page",bookPage);
//        System.out.println(bookPage.getCurrent());
//        System.out.println(bookPage.getRecords().size());
        /**
         * 2021-11-18 21:02:01.321 ERROR 9272 --- [nio-8082-exec-4] org.thymeleaf.TemplateEngine             : [THYMELEAF][http-nio-8082-exec-4] Exception processing template "main": An error happened during template parsing (template: "class path resource [templates/main.html]")
         *
         * org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/main.html]")
         * 	at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parse(AbstractMarkupTemplateParser.java:241) ~[thymeleaf-3.0.12.RELEASE.jar:3.0.12.RELEASE]
         * Caused by: org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "page.getCurrent()" (template: "main" - line 86, col 17)
         * 这里不能使用重定向：否则前端数据不能通过thymeleaf获取
         */
//        return "redirect:/main";
        return "main";
    }

    @GetMapping("/add")
    public String toAddPage(){
        return "add";
    }

    @PostMapping(value = {"/add"})
    public String add(Book book,
//                      @RequestParam(value = "pn",defaultValue = "1")Integer pn,
//                      Model model,
                      HttpServletRequest request,
                      RedirectAttributes redirectAttributes){
        //model里面的值只能存放当次请求的，需要放session里面才行
//        Object pn = model.getAttribute("pn");
        Object pn = request.getSession().getAttribute("pn");
        System.out.println("pn的值："+pn);
//        boolean save = bookService.save(book);
        boolean save = bookService.saveOrUpdate(book);
        redirectAttributes.addAttribute("pn",pn);
//        if (save){
            //return "main";
//            http://localhost:8082/showemp?pn=191
        return "redirect:/showemp";
            /**
             * http://localhost:8082/add
             * 遇到了500相关错误
             */
            //return "showemp";
//        }
//        return "add";
    }

    @GetMapping("/delete/{id}")
    public String delete(@PathVariable("id")Integer id,
                         @RequestParam(value = "pn",defaultValue = "1")Integer pn,
                         RedirectAttributes redirectAttributes){
        boolean removeById = bookService.removeById(id);
        redirectAttributes.addAttribute("pn",pn);
        return "redirect:/showemp";
    }

    /**
     * 修改页面和添加方法用同一个，方法也是用同一个
     * 去往添加或者修改页面也可以用同一个方法，但是为了在添加的时候add页面为空，修改的时候add页面有数据，使用了两个方法
     * 当方法为空的时候前端取值需要注意：<inputtype="text"th:value="${book?.id}"name="id"> 有一个问号
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/update/{id}")
    public String toUpdatePage(@PathVariable("id")Integer id,
                               Model model,
                               HttpServletRequest request,
                               @RequestParam(value = "pn",defaultValue = "1")Integer pn){
        Book byId = bookService.getById(id);
        model.addAttribute("book",byId);
        //model里面不能不同请求之间传值，需要放session里
//        model.addAttribute("pn",pn);
        request.getSession().setAttribute("pn",pn);
        return "add";
    }
    /*@PostMapping("/update")
    public String update(){
        return "redirect:/showemp";
    }*/
}
